<?php

namespace App;

use App\Model\Business;
use App\Model\Contract;
use App\Model\Device;
use App\Model\Fdd;
use App\Model\Message;
use App\Model\Order;
use App\Model\Tenant;
use App\Model\UserBank;
use App\Model\UserCashLog;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    public $table = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nickname', 'openid', 'password', 'avatar', 'mobile'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    public $casts = [
        'created_at' => 'date:Y-m-d H:i:s'
    ];

    public function business()
    {
        return $this->hasOne(Business::class);
    }

    public function tenant()
    {
        return $this->hasOne(Tenant::class);
    }

    public function fdd()
    {
        return $this->hasOne(Fdd::class);
    }

    public function bank()
    {
        return $this->hasOne(UserBank::class,'account_id','account_id');
    }

    public function contract()
    {
        return $this->hasOne(Contract::class, 'mobile', 'mobile');
    }

    public function message()
    {
        return $this->hasMany(Message::class);
    }

    public function cashLog()
    {
        return $this->hasMany(UserCashLog::class);
    }


    public function devices()
    {
        return $this->belongsToMany(Device::class, 'user_device');
    }


    public function recentCashLogs()
    {
        return $this->cashLog()
            ->whereDate('created_at', '>=', now()->subDays(30));
    }

    public function tevice(){
        return $this->hasOne(Device::class,'mobile','mobile')
            ->select(['id', 'mobile', 'du','house_id'])
            ->whereNotNull('mobile')
            ->where('mobile', '<>', '');
    }

}
